package com.hj.dao;

import com.hj.domain.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;

public interface CustomerDao extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> {


    /**
     * 使用Spring Data JPA的JPQL、原生SQL查询语句时，需要自定义方法
     * JPQL/SQL查询语句使用@Query注解进行封装
     * @Query注解中的nativeQuery取值为true|false，默认值为false
     *     true：使用本地查询 即使用原生SQL语句查询
     *     false:不使用本地查询  即使用JPQL语句查询
     *
     */

    /**
     * 根据客户名称查询客户信息(一个占位符)
     * JPQL语句： from Customer where custName = ?
     */
    @Query("from Customer where custName = ?1")
    Customer findCustomerByCustName(String custName);

    /**
     * 根据客户名称和ID查询客户信息(多个占位符)
     * JPQL语句： from Customer where custName = ? and custId = ?
     */
    @Query("from Customer where custName = ?2 and custId = ?1")
    Customer findCustomerByNameAndId(Long id,String name);

    /**
     * 根据ID更新客户名称
     * JPQL语句： update Customer set custName = ? where custId = ?
     */
    @Query("update Customer set custName = ?2 where custId = ?1")
    @Modifying //标注这是更新
    @Transactional //使用事务,否则会报错
    void updateCustomerById(Long id,String name);


    /**
     * 根据客户名称查询客户信息(一个占位符)
     * SQL语句： from Customer where custName = ?
     */
    @Query(value = "select * from cst_customer where cust_name = ?1",nativeQuery = true)
    Customer findCustomerBySql(String name);

    /**
     * 使用方法名查询，此时不需要写JPQL和SQL语句
     */

    Customer findByCustName(String name);


}
